package com.daisuxia.risk.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.apache.commons.lang3.StringUtils;

public class FormulaUtil {

	/**
	 * 检查公式语法，把所有的唯一标识去掉，刊登否正常得出结果
	 * 
	 * @param formula
	 * @return
	 */
	public static Object checkFormula(String formula) {
		formula = formula.replaceAll(ConstantRisk.RULE_PREFIX, "");
		return calFormula(formula);
	}

	public static Object calFormula(String formula) {
		JexlContext ctxt = new MapContext();
		JexlEngine jexl = new JexlEngine();
		Expression expr = jexl.createExpression(formula);
		return expr.evaluate(ctxt);
	}

	public static void main(String[] args) {
		// String formula = "if('sdfds'=='sdfds'){return true;}";
		// String formula = "if('0.0'=='0'){return true;}";
		// String formula = "if('0'==0){return true;}";
		// String formula = "if('81.5'>=-5){return true;}";
		// JexlContext ctxt = new MapContext();
		// JexlEngine jexl = new JexlEngine();
		// Expression expr = jexl.createExpression(formula);
		// System.out.println(expr.evaluate(ctxt));
		// String regex = "/^[\u4e00-\u9fa5]/";
		// String str = "1a啊";
		// Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		// Matcher m = p.matcher(str);
		// if (m.find()) {
		// System.out.println("1");
		// }else {
		// System.out.println("2");
		// }
		// System.out.println("妈🌹".replaceAll("[^\\u0000-\\uFFFF]", ""));
		// String source = "妈🌹";
		// if (source != null) {
		// Pattern emoji = Pattern
		// .compile(
		// "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
		// Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
		// Matcher emojiMatcher = emoji.matcher(source);
		// if (emojiMatcher.find()) {
		// source = emojiMatcher.replaceAll("*");
		// }
		// System.out.println(source);
		// }
//		 String evidence =
//		 "联系列表:[中信信用卡]主叫7次共38.52分钟，被叫0次共0.0分钟;[你我贷]主叫1次共3.3分钟，被叫0次共0.0分钟;[信用金]主叫9次共13.88分钟，被叫0次共0.0分钟;[北京宜车贷]主叫1次共8.5分钟，被叫0次共0.0分钟;[平安易贷]主叫1次共1.12分钟，被叫6次共40.6分钟;[店主237608]主叫1次共0.95分钟，被叫2次共1.92分钟;[民生银行信用卡]主叫10次共13.48分钟，被叫0次共0.0分钟;[玖富理财]主叫1次共1.68分钟，被叫0次共0.0分钟;[罗经理]主叫1次共3.47分钟，被叫0次共0.0分钟";
		String evidence = "联系列表:[牛娃互联网金融]主叫1次共1.4分钟，被叫0次共0.0分钟";
		String flag = "次";
		String flag2 = "主叫";
		String flag3 = "被叫";
		String flag4 = "联系列表";
		if (evidence.indexOf(flag4) >= 0) {
			String[] eStrings = evidence.split(";");
			for (String string : eStrings) {
				while (string.indexOf(flag2) >= 0) {
					int index2 = string.indexOf(flag2);
					int index1 = string.indexOf(flag);
					System.out.println(string.substring(
							index2 + flag2.length(), index1));
					string = string.substring(index1 + 1);
					int index3 = string.indexOf(flag3);
					int index4 = string.indexOf(flag);
					System.out.println(string.substring(
							index3 + flag3.length(), index4));
//					if (index1 + 1 <= string.length()) {
						string = string.substring(index4 + 1);
//					}
				}
			}
		}
	}
}
